
%omitDiameterEffect is set to "true" if you want to not count that in this

function [locomotioneffect,pupileffect,diametereffect,mid] = getMouseSpecificEffects(datatable,vesselClass,vasomotionClass,pthresh,omitDiameterEffect)

if nargin < 5 || isempty(omitDiameterEffect)
    omitDiameterEffect = false;
end
[mid,~,mididx] = unique(datatable.mouseID,'stable');
locomotioneffect = zeros(length(mid),1);
pupileffect = zeros(length(mid),1);
diametereffect = zeros(length(mid),1);
nofit = false;
if ~omitDiameterEffect
    for n = 1:length(mid)
        curtable = datatable(mididx==n,:);
        switch vesselClass
            case 'diving'
                switch vasomotionClass
                    case 'maxDilation'
                        lme = fitlme(curtable,'maxDilation ~ locomotion + maxPupilRadius + restingDiameter + (1|roi_stim_clusters)',...
                            'FitMethod','REML');  %including a resting diameter term
                    case 'totalDilation'
                        lme = fitlme(curtable,'totalDilation ~ locomotion + maxPupilRadius + restingDiameter + (1|roi_stim_clusters)',...
                            'FitMethod','REML');  %including a resting diameter term
                end
            case 'pia'
                switch vasomotionClass
                    case 'maxDilation'
                        try
                            lme = fitlme(curtable,'maxDilation ~ locomotion + maxPupilRadius + restingDiameter + (1|stim_roi_id:vessel_stim_ID)',...
                                'FitMethod','REML');  %including a resting diameter term
                        catch
                            disp('not enough vessels to fit mouse behavior')
                            nofit = true;
                            locomotioneffect(n) = 0;pupileffect = 0;diametereffect = 0; continue;
                        end
                    case 'totalDilation'
                        lme = fitlme(curtable,'totalDilation ~ locomotion + maxPupilRadius + restingDiameter + (1|stim_roi_id:vessel_stim_ID)',...
                            'FitMethod','REML');  %including a resting diameter term
                end
        end
    
        % set relevant offsets from the behavior regression:
        [~,~,STATS] = fixedEffects(lme);
        if STATS{2,6} > pthresh
            locomotioneffect(n) = 0;
        else
            locomotioneffect(n) = STATS{2,2};
        end
        if STATS{4,6} > pthresh
            pupileffect(n) = 0;
        else
            pupileffect(n) = STATS{4,2};
        end
        disp(['pupil effect field is ',STATS{4,1}])
        if STATS{3,6} > pthresh
            diametereffect(n) = 0;
        else
            diametereffect(n) = STATS{3,2};
        end
        if diametereffect(n) > 0;diametereffect(n) = 0;end
    end
else
    for n = 1:length(mid)
        curtable = datatable(mididx==n,:);
        switch vesselClass
            case 'diving'
                switch vasomotionClass
                    case 'maxDilation'
                        lme = fitlme(curtable,'maxDilation ~ locomotion + maxPupilRadius + (1|roi_stim_clusters)',...
                            'FitMethod','REML');  %including a resting diameter term
                    case 'totalDilation'
                        lme = fitlme(curtable,'totalDilation ~ locomotion + maxPupilRadius + (1|roi_stim_clusters)',...
                            'FitMethod','REML');  %including a resting diameter term
                end
            case 'pia'
                switch vasomotionClass
                    case 'maxDilation'
                        try
                            lme = fitlme(curtable,'maxDilation ~ locomotion + maxPupilRadius + (1|stim_roi_id:vessel_stim_ID)',...
                                'FitMethod','REML');  %no resting diameter term
                        catch
                            disp('not enough vessels to fit mouse behavior')
                            nofit = true;
                            locomotioneffect(n) = 0;pupileffect = 0;diametereffect = 0; continue;
                        end
                    case 'totalDilation'
                        lme = fitlme(curtable,'totalDilation ~ locomotion + maxPupilRadius + (1|stim_roi_id:vessel_stim_ID)',...
                            'FitMethod','REML');  %including a resting diameter term
                end
        end
    
        % set relevant offsets from the behavior regression:
        [~,~,STATS] = fixedEffects(lme);
        if STATS{2,6} > pthresh
            locomotioneffect(n) = 0;
        else
            locomotioneffect(n) = STATS{2,2};
        end
        if strcmp(STATS{3,1},'maxPupilRadius')
            if STATS{3,6} > pthresh
                pupileffect(n) = 0;
            else
                pupileffect(n) = STATS{3,2};
            end
            disp(['pupil effect field is ',STATS{3,1}])
        else
            if STATS{4,6} > pthresh
                pupileffect(n) = 0;
            else
                pupileffect(n) = STATS{4,2};
            end
            disp(['pupil effect field is ',STATS{4,1}])
        end
        diametereffect(n) = 0;
    end
end